package hashDemo;

import entity.Person;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Ajie
 * @date 2020-09-10
 * @function  测试 弱引用 key value 都被回收。
 */
public class LinkedHashMapDemo {
    private static final Logger logger = Logger.getLogger(LinkedHashMapDemo.class);

    public static void main(String[] args) {
//    public static void main(FaceFeatureRef featureRef,IBinder token){

//        autoGc();//WeakReference

        autoDelOldValue();

//        LruLinkedHashMap<WeakReference<FaceBufferRef>,WeakReference<FaceFeatureRef>> mbufferToFeatureRefMap = new LruLinkedHashMap<>(8);
//        Iterator<Map.Entry<WeakReference<FaceBufferRef>, WeakReference<FaceFeatureRef>>> iterator = mbufferToFeatureRefMap.entrySet().iterator();
//        while (iterator.hasNext()){
//            Map.Entry<WeakReference<FaceBufferRef>, WeakReference<FaceFeatureRef>> next = iterator.next();
//            WeakReference<FaceBufferRef> key = next.getKey();
//            WeakReference<FaceFeatureRef> value = next.getValue();
//            if (key!=null && key == args){
//                return value.get();
//            }
//
//        }

    }

    private static void autoDelOldValue() {
        LruCacheLinkedHashMap<WeakReference<Person>,WeakReference<Man>> map = new LruCacheLinkedHashMap<>(2);
        Person person = new Person("16");
        WeakReference<Person> personWeakReference = new WeakReference<>(new Person("10"));
        map.put(new WeakReference<>(new Person("1")),new WeakReference<>(new Man("1")));
        map.put(new WeakReference<>(new Person("15")),new WeakReference<>(new Man("15")));
        map.put(new WeakReference<>(new Person("14")),new WeakReference<>(new Man("14")));
        map.put(new WeakReference<>(person),new WeakReference<>(new Man("16")));
        map.put(personWeakReference,new WeakReference<>(new Man("10")));

        Iterator<Map.Entry<WeakReference<Person>, WeakReference<Man>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<WeakReference<Person>, WeakReference<Man>> next = iterator.next();
            System.out.println("LinkedHashMapDemo:main:iterator:--->"+next.getKey().get().toString()+" "+next.getValue().get().toString());
        }

        Man man = getValue(map,person);

        System.out.println("LinkedHashMapDemo:autoDelOldValue:getValue::::"+man.toString());
        WeakReference<Man> manRef = map.get(personWeakReference);

        System.out.println("LinkedHashMapDemo:autoDelOldValue:"+manRef.get().toString());
        WeakReference<Man> manWeakReference = map.get(person);
        System.out.println("LinkedHashMapDemo:autoDelOldValue:"+manWeakReference.get().toString());
    }

    private static Man getValue(LruCacheLinkedHashMap<WeakReference<Person>, WeakReference<Man>> map, Person person) {
        if (map == null || map.size()<1) return null;
        Iterator<Map.Entry<WeakReference<Person>, WeakReference<Man>>> iterator = map.entrySet().iterator();
        Man man = null;
        while (iterator.hasNext()){
            Map.Entry<WeakReference<Person>, WeakReference<Man>> next = iterator.next();
            WeakReference<Person> key = next.getKey();
            WeakReference<Man> value = next.getValue();
            if (key!=null && key.get() == person){
                return value.get();
            }
        }
        return man;
    }

    private static void autoGc() {
        LinkedHashMap<WeakReference<Person>,WeakReference<Man>> map = new LinkedHashMap<>();
        Person person = new Person();
        Man aaa = new Man("aaa");
        map.put(new WeakReference<>(person),new WeakReference<>(aaa));

        person = null;
        aaa = null;
        System.gc();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.gc();

        Iterator<Map.Entry<WeakReference<Person>, WeakReference<Man>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<WeakReference<Person>, WeakReference<Man>> next = iterator.next();
//            System.out.println("LinkedHashMapDemo:main:iterator:--->"+next.getKey().get().toString()+" "+next.getValue().get().toString());
            if (next.getKey().get()==null && next.getValue().get()==null){
                System.out.println("LinkedHashMapDemo:main:😄😄😄😄😄😄😄 key value both is null");
                continue;
            } else if(next.getKey().get()==null){
                System.out.println("LinkedHashMapDemo:main:😄key is null");
                continue;
            }else if(next.getValue().get()==null){
                System.out.println("LinkedHashMapDemo:main:😄 value  is null");
                continue;
            }
            System.out.println("LinkedHashMapDemo:main:iterator:--->"+next.getKey().get().toString()+" "+next.getValue().get().toString());
        }
    }

    @Test
    public void putTest(){
        LinkedHashMap<String, String> linkedMap = new LinkedHashMap<>();
        String put = linkedMap.put("a-key", "a-value");
        logger.info("putTest: put result:"+put);
        String put2 = linkedMap.put("a-key", "a-value");
        logger.info("putTest: put re result:"+put2);
        String put3 = linkedMap.put("a-key", "a-value-1");//返回 上一个 值 a-value
        logger.info("putTest: put re2 result:"+put3);

    }

    static class Man{
        String name;

        public Man(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Man{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
